package com.kucai.yifaqapp.ui.fragment;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kucai.yifaqapp.R;
import com.kucai.yifaqapp.global.HttpAdress;
import com.kucai.yifaqapp.global.Info;
import com.kucai.yifaqapp.model.CustomObserver;
import com.kucai.yifaqapp.model.network.HttpJsonUtils;
import com.kucai.yifaqapp.model.network.entity.res.IsHasQipaiBean;
import com.kucai.yifaqapp.model.network.entity.res.ResMoney;
import com.kucai.yifaqapp.ui.activity.BankidActivity;
import com.kucai.yifaqapp.ui.activity.ChangeSkinActivity;
import com.kucai.yifaqapp.ui.activity.ChessActivity;
import com.kucai.yifaqapp.ui.activity.IdearActivity;
import com.kucai.yifaqapp.ui.activity.MessageActivity;
import com.kucai.yifaqapp.ui.activity.NowActivity;
import com.kucai.yifaqapp.ui.activity.PasswordActivity;
import com.kucai.yifaqapp.ui.activity.PasswordActivitySecond;
import com.kucai.yifaqapp.ui.activity.RecommendActivity;
import com.kucai.yifaqapp.ui.activity.RecrdActivity;
import com.kucai.yifaqapp.ui.activity.UserActivity;
import com.kucai.yifaqapp.ui.activity.WebActivity;
import com.kucai.yifaqapp.util.EventBus.goTomoneyEvent;
import com.kucai.yifaqapp.util.OnItemClickListener;
import com.kucai.yifaqapp.widget.AlertDialogIos;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/26.
 */

public class LauncherViewPagerUserFragment1 extends BaseFragmentSimple {
    LinearLayout skinbg;
    TextView usernameTv;
    TextView moneyTv;
    ImageView leftBt;
    ImageView rightBt;
    TextView appname;
    ImageView refreshyue;
    ImageView Recommend;
    RecyclerView content;//个人中心主题数据
    private AlertDialogIos myDialog;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        skinbg = view.findViewById(R.id.skinbg);
        usernameTv = view.findViewById(R.id.username_tv);
        moneyTv = view.findViewById(R.id.money_tv);
        leftBt = view.findViewById(R.id.left_bt);
        rightBt = view.findViewById(R.id.right_bt);
        appname = view.findViewById(R.id.appname);
        refreshyue = view.findViewById(R.id.refreshyue);
        Recommend = view.findViewById(R.id.Recommend);
        content = view.findViewById(R.id.content);
        initTitle();
        initRecyclewView();
    }

    private void initTitle() {
        if (null != Info.user) {
            if (null != Info.user.userName) {
                usernameTv.setText("您好!  "+Info.user.userName+"");
            }
            if (null != Info.user.money) {
                moneyTv.setText("用户余额:" + Info.user.money + "");
            }
        } else {
            moneyTv.setText("用户余额:0.00 ");
        }
        leftBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new goTomoneyEvent("1"));
            }
        });
        rightBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Info.seviceurl != null) {
                    Intent intent = new Intent(getContext(), WebActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("url", Info.seviceurl);
                    bundle.putString("title", "在线客服");
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
        refreshyue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getUserMoney();
            }
        });
    }

    private void initRecyclewView() {
        //禁止滑动
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        //添加布局管理器
        content.setLayoutManager(gridLayoutManager);
        content.setHasFixedSize(true);
        initData();
        //开始设置adapter
        Adapter adapter = new Adapter();
        content.setAdapter(adapter);
    }

    private List<String> titles = new ArrayList<>();
    private List<Integer> iconlist = new ArrayList<>();

    private void initData() {
        //初始化content数据
        if (IsHasQipaiBean.ishasqipai) {
            titles.add("棋牌额度转换");
        }
        titles.add("个人资料");
        titles.add("登录密码");
        titles.add("我的消息");
        titles.add("资金管理");
        titles.add("取款密码");
        titles.add("银行账号");
        titles.add("今日已结");
        titles.add("下注记录");
        titles.add("建议投诉");
        titles.add("更换皮肤");
        titles.add("版本号");
        if (Info.user != null) {
            if (Info.user.promoter != null && Info.user.promoter.equals("1")) {
                titles.add("我的推荐");
            }
        }
        if (IsHasQipaiBean.ishasqipai) {
            iconlist.add(R.mipmap.download);
        }
        iconlist.add(R.mipmap.menuprofile);
        iconlist.add(R.mipmap.menumodifypwd);
        iconlist.add(R.mipmap.menumessage);
        iconlist.add(R.mipmap.menufunds);
        iconlist.add(R.mipmap.menutransfer);
        iconlist.add(R.mipmap.menubankaccount);
        iconlist.add(R.mipmap.menurecord);
        iconlist.add(R.mipmap.menurecorda);
        iconlist.add(R.mipmap.menufeedback);
        iconlist.add(R.mipmap.king);
        iconlist.add(R.mipmap.newapp);
        if (Info.user != null) {
            if (Info.user.promoter != null && Info.user.promoter.equals("1")) {
                iconlist.add(R.mipmap.my_tuijian);
            }
        }
    }

    private void getUserMoney() {
        HttpJsonUtils.getHttpApiInstance()
                .getmoney(HttpAdress.getuseAdress() + "api/m/user/getMoney", Info.user.token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CustomObserver<ResMoney>(LauncherViewPagerUserFragment1.this) {
                    @Override
                    protected void onCustomNext(ResMoney resOods) {
                        if (resOods.code == 0) {
                            moneyTv.setText("用户余额：" + resOods.data.balance);
                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {
                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                    }
                });
    }

    @Override
    protected void ChangeSkin() {
        switch (SkinId) {
            case 17771:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
                break;
            case 17772:
                skinbg.setBackgroundResource(R.mipmap.skin_bg2);
                break;
            case 17773:
                skinbg.setBackgroundResource(R.mipmap.skin_bg3);
                break;
            default:
                skinbg.setBackgroundResource(R.mipmap.skin_bg1);
        }
    }


    @Override
    protected int setContentView() {
        return R.layout.fragment_user;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (null != Info.user) {
                if (null != Info.user.userName) {
                    usernameTv.setText("您好!  " + Info.user.userName);
                }
                if (null != Info.user.money) {
                    moneyTv.setText("用户余额:" + Info.user.money + "");
                }
            } else {
                moneyTv.setText("用户余额:0.00 ");
            }
            titles.clear();
            iconlist.clear();
            initData();

        }
    }


    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public Adapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.game_item2, parent, false));
        }

        @Override
        public void onBindViewHolder(final Adapter.MViewHolder holder, final int position) {
            holder.game_iv.setImageResource(iconlist.get(position));
            holder.geme_tv.setText(titles.get(position));
            holder.item_1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    switch (titles.get(position)) {
                        case "棋牌额度转换":
                            if (Info.user.userName != null) {
                                startActivity(new Intent(getContext(), ChessActivity.class));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                                return;
                            }
                            if (!IsHasQipaiBean.ishasqipai) {
                                return;
                            }
                            break;
                        case "个人资料":
                            if (Info.user.userName != null) {
                                startActivity(new Intent(getContext(), UserActivity.class));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "登录密码":
                            if (Info.user.userName != null) {
                                startActivity(new Intent(getContext(), PasswordActivity.class));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "我的消息":
                            if (Info.user.userName != null) {
                                startActivity(new Intent(getContext(), MessageActivity.class));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "资金管理":
                            if (Info.user.userName != null) {
                                EventBus.getDefault().post(new goTomoneyEvent("3"));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "取款密码":
                            if (Info.user.userName != null) {
                                startActivity(new Intent(getContext(), PasswordActivitySecond.class));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "银行账号":
                            if (Info.user.userName != null) {
                                startActivity(new Intent(getContext(), BankidActivity.class));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "今日已结":
                            startActivity(new Intent(getContext(), NowActivity.class));
                            break;
                        case "下注记录":
                            if (Info.user.userName != null) {
                                startActivity(new Intent(getContext(), RecrdActivity.class));
                            } else {
                                Toast.makeText(getContext(), "试玩账号无权访问，请先注册", Toast.LENGTH_SHORT).show();
                            }
                            break;
                        case "建议投诉":
                            startActivity(new Intent(getContext(), IdearActivity.class));
                            break;
                        case "更换皮肤":
                            startActivity(new Intent(getContext(), ChangeSkinActivity.class));
                            break;
                        case "版本号":
                            //  throw new RuntimeException("Stub!");  主动抛出异常测试使用
                            myDialog = new AlertDialogIos(getActivity()).builder();
                            PackageInfo packageInfo = null;
                            try {
                                packageInfo = getContext().getApplicationContext()
                                        .getPackageManager()
                                        .getPackageInfo(getContext().getPackageName(), 0);
                                //热更新不能修改版本号所以将packageInfo.versionName写死为1.0.6
                                myDialog.setGone().setMsg("当前版本为:" + getString(R.string.AppVersion)).setNegativeButton("取消", R.color.skyblue, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        myDialog.dismiss();
                                    }
                                }).setPositiveButton("确定", R.color.skyblue, new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        myDialog.dismiss();
                                    }
                                }).show();
                            } catch (PackageManager.NameNotFoundException e) {
                                e.printStackTrace();
                            }
                            break;
                        case "我的推荐":
                            startActivity(new Intent(getContext(), RecommendActivity.class));
                            break;
                    }
                }
            });
        }

        @Override
        public int getItemCount() {
            if (iconlist.size() != 0) {
                return iconlist.size();
            } else {
                return 0;
            }
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView geme_tv;
            ImageView game_iv;
            RelativeLayout item_1;

            MViewHolder(View itemView) {
                super(itemView);
                geme_tv = (TextView) itemView.findViewById(R.id.geme_tv);
                game_iv = (ImageView) itemView.findViewById(R.id.game_iv);
                item_1 = itemView.findViewById(R.id.item_1);
            }
        }
    }
}
